
package jfireeagle.xml;

import jfireeagle.*;

import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;

public class UserConverter extends com.thoughtworks.xstream.converters.reflection.ReflectionConverter
{

    public UserConverter(Mapper m)
	{
    	super(m, new PureJavaReflectionProvider());
	}

	public Object unmarshal(HierarchicalStreamReader reader,
			UnmarshallingContext context)
	{
		User u = (User) super.unmarshal(reader, context);
		
		String token = reader.getAttribute("token");
		u.setToken(token);
		
		String locatedAt = reader.getAttribute("located-at");
		
		if (locatedAt != null)
		{
			CalendarConverter con = new CalendarConverter();
			u.setLocatedAt( (java.util.Calendar) con.fromString(locatedAt));
		}
		
        return u;
	}

	public boolean canConvert(Class type)
	{
		return User.class.isAssignableFrom(type);
	}

	public void marshal(Object source, HierarchicalStreamWriter writer,
			MarshallingContext context)
	{
		// not implemented
		
	}

}
